/*
 * main.cpp
 *
 *  Created on: Jan 14, 2014
 *      Author: killerg
 */



#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <unordered_set>
#include <set>
#include <sstream>

#include "share/util.h"

struct para_t {
    int num;
    int reward;
    int mask;
};

int main(int argc, char** argv) {
    int key = -1;
    std::vector<para_t> paras;
    const char* name = NULL;

    if (argc < 5 || argc % 2 == 0) {
        printf("usage: cmd filename key prefix1 num1 reward1 ...\n");
        return -1;
    }

    name = argv[1];
    key = atoi(argv[2]);
    int total = 0;
    for (int i=3; i<argc; ) {
        para_t para;
        para.num = atoi(argv[i++]);
        para.reward = atoi(argv[i++]);
        para.mask = paras.size();

        paras.emplace_back(para);

        total += para.num;
    }

    std::string txt_path = fmt_cstr("%s/bin/%s", get_deploy(), name);
    std::string cfg_path = fmt_cstr("%s/cfg/%s", get_deploy(), name);
    Json::Value cfg;

    Json::Value &codes = cfg["item"];

    std::stringstream txt;
    std::set<int> set;
    while (set.size() < total) {
        int num = rand() % 0xffffffff;
        set.insert(num);
    }

    auto num_itr = set.begin();
    for (auto itr : paras) {
        for (int i=0; i<itr.num; ++i) {
            if (num_itr == set.end()) {
                printf("num not enough\n");
                return -1;
            }

            Json::Value code;
            code["code"] = fmt_cstr("%08x", *num_itr);
            code["reward"] = itr.reward;
            code["mask"] = itr.mask;
            codes.append(code);
            txt << code["code"].asCString() << std::endl;
            ++num_itr;
        }
    }

    save_file(cfg_path.c_str(), to_cstr(cfg));
    save_file(txt_path.c_str(), txt.str().c_str());
}
